package com.cskaoyan.controller;
/*
 * @author 魏骏杰
 */

import com.cskaoyan.bean.basebean.BaseRespVo;
import com.cskaoyan.bean.keyword.KeyWordBO;
import com.cskaoyan.bean.keyword.KeyWordVO;
import com.cskaoyan.bean.zhao_order_bean.BasePageData;
import com.cskaoyan.service.KeyWordService;
import com.cskaoyan.utils.MarketPageUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("admin/keyword")
public class KeyWordController {
    @Autowired
    KeyWordService keyWordService;

    @RequiresPermissions(value = "admin:keyword:list")
    @RequestMapping("list")
    public BaseRespVo list(KeyWordBO keyWordBO) {
        MarketPageUtil.startPage(keyWordBO);
        List<KeyWordVO> list = keyWordService.getKeyWordList(keyWordBO);
        BasePageData<KeyWordVO> keyWordListPage = BasePageData.ok(list);
        return BaseRespVo.ok(keyWordListPage);
    }

    @RequiresPermissions(value = "admin:keyword:create")
    @RequestMapping("create")
    public BaseRespVo create(@RequestBody KeyWordBO keyWordBO) {
        if (!keyWordService.hasExistence(keyWordBO)) {
            return BaseRespVo.ok(keyWordService.createNewKeyWord(keyWordBO));
        }
        return BaseRespVo.fail("关键词已存在！");
    }

    @RequiresPermissions(value = "admin:keyword:update")
    @RequestMapping("update")
    public BaseRespVo update(@RequestBody KeyWordBO keyWordBO) {
        if (!keyWordService.hasExistence(keyWordBO)) {
            return BaseRespVo.ok(keyWordService.updateNewKeyWord(keyWordBO));
        }
        return BaseRespVo.fail("关键词已存在！");
    }

    @RequiresPermissions(value = "admin:keyword:delete")
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody KeyWordBO keyWordBO) {
        keyWordService.deleteKeyWord(keyWordBO);
        return BaseRespVo.ok();
    }
}
